package com.example.ui.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.ButtonBarLayout;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.constant.live.LivesBean;
import com.example.ui.R;
import com.example.widget.media.IjkVideoView;
import com.squareup.picasso.Picasso;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import tv.danmaku.ijk.media.player.IMediaPlayer;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;

public class ViedoActivity extends AppCompatActivity implements IMediaPlayer.OnPreparedListener
{
    @BindView(R.id.iv_toolbar_bg)
    ImageView ivToolbarBg;
    @BindView(R.id.ivv_live)
    IjkVideoView ivvLive;
    @BindView(R.id.ib_toolbar_back)
    ImageButton ibToolbarBack;
    @BindView(R.id.playButton)
    ButtonBarLayout playButton;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.toolbar_layout)
    CollapsingToolbarLayout toolbarLayout;
    @BindView(R.id.app_bar)
    AppBarLayout appBar;
    @BindView(R.id.viedoInfo)
    TextView viedoInfo;
    @BindView(R.id.fab)
    FloatingActionButton fab;
    @BindView(R.id.clViedo)
    CoordinatorLayout clViedo;
    private String url;
    private String name;
    private String cover;
    private Intent intent;
    private LivesBean livesBean;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viedo);
        ButterKnife.bind(this);
        initView();
        intent = getIntent();
        livesBean = (LivesBean) intent.getSerializableExtra("data");
        Log.i("test",livesBean.toString()+livesBean.getOwner().toString());
        url = livesBean.getCover().getSrc();
        name = livesBean.getTitle();
        cover = livesBean.getCover().getSrc();
        Picasso.with(this)
                .load(cover)
                .placeholder(R.mipmap.bili_drawerbg_not_logined)
                .error(R.mipmap.bili_drawerbg_logined)
                .into(ivToolbarBg);
        ivvLive.setOnPreparedListener(this);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle(name);
        setSupportActionBar(toolbar);
    }

    private void initView()
    {
    }

    @Override
    protected void onRestart()
    {
        intent = getIntent();
        livesBean = (LivesBean) intent.getSerializableExtra("data");
        url = livesBean.getCover().getSrc();
        name = livesBean.getTitle();
        cover = livesBean.getCover().getSrc();
    }

    @Override
    public void onPrepared(IMediaPlayer iMediaPlayer)
    {
        ivvLive.start();
    }

    private void ivvShow()
    {
        ivvLive.setVisibility(View.VISIBLE);
        ivToolbarBg.setVisibility(View.GONE);
        fab.setVisibility(View.GONE);
        toolbar.setVisibility(View.GONE);
    }

    private void ivvdisapper()
    {
        ivvLive.setVisibility(View.GONE);
        ivToolbarBg.setVisibility(View.VISIBLE);
        fab.setVisibility(View.VISIBLE);
        toolbar.setVisibility(View.VISIBLE);
    }

    @OnClick({R.id.ivv_live, R.id.ib_toolbar_back, R.id.fab})
    public void onClick(View view)
    {
        switch (view.getId())
        {
            case R.id.ivv_live:
                ivvdisapper();
                break;
            case R.id.ib_toolbar_back:
                finish();
                break;
            case R.id.fab:
                ivvShow();
                IjkMediaPlayer.loadLibrariesOnce(null);
                IjkMediaPlayer.native_profileBegin("libijkplayer.so");
                ivvLive.setVideoPath(livesBean.getPlayurl());
                ivvLive.start();
                break;
        }
    }

//    @OnClick(R.id.fab)
//    public void onClick()
//    {
//    }


    @Override
    public void onBackPressed()
    {
        super.onBackPressed();
        if (ivvLive == null)
        {
            this.finish();
        } else if (ivvLive.isPlaying())
        {
            ivvLive.stopPlayback();
            this.finish();
        }
    }

    @OnClick(R.id.ib_toolbar_back)
    public void onClick()
    {
        Toast.makeText(this,"ib_toolbar_back",Toast.LENGTH_SHORT).show();
    }
}
